#================================================
package MSN3::Notification::P2P;
#================================================

use strict;
use warnings;

# For Debugging
use Data::Dumper;

# General
use Switch;

# For DP
use Digest::SHA1 qw(sha1 sha1_hex sha1_base64);
use MIME::Base64;
use HTML::Entities;

# For errors
use MSN3::Util;

sub checksum { my $o = tell(DATA); seek DATA,0,0; local $/; my $t = unpack("%32C*",<DATA>) % 65535;seek DATA,$o,0; return $t;};

sub new
{
    my $class = shift;
    my $msn = shift;

    my $self =
    {
        MSN          => $msn,

        Objects      => {},
        CurrentDP    => '',

        @_
    };
    bless($self, $class);

    return $self;
}

sub DESTROY
{
    my $self = shift;

    # placeholder for possible destructor code
}

sub AUTOLOAD
{
    my $self = shift;

    $self->error( "method $MSN3::Notification::P2P::AUTOLOAD not defined" ) if( $self->{AutoloadError} );
}

#================================================
# Error / Debugging
#================================================

sub debug
{
    my $self = shift;

    return $self->{MSN}->debug( @_ );
}

sub error
{
    my $self = shift;

    return $self->{MSN}->error( @_ );
}

#================================================
# Send functions
#================================================

sub send
{
    my $self = shift;

    return $self->{MSN}->getNotification()->send( @_ );
}

sub sendraw
{
    my $self = shift;

    return $self->{MSN}->getNotification()->sendraw( @_ );
}

sub sendput
{
    my $self = shift;

    return $self->{MSN}->getNotification()->sendput( @_ );
}

sub sendsdg
{
    my $self = shift;

    return $self->{MSN}->getNotification()->sendput( @_ );
}

sub senddel
{
    my $self = shift;

    return $self->{MSN}->getNotification()->senddel( @_ );
}

sub _send
{
    my $self = shift;

    return $self->{MSN}->getNotification()->_send( @_ );
}

#================================================
# Dispatch a p2p event
#================================================

sub dispatch
{
    my $self = shift;
    my $header = shift || return;
    my $msg = shift || return;

    if( $header->{'Content-Type'} =~ /text\/plain/ ) {}
    elsif( $header->{'Content-Type'} =~ /application\/x-msnmsgrp2p/ )
    {
    }
}

#================================================
# P2P functions
#================================================


#================================================
# Misc functions
#================================================

sub getObjectLocation
{
    my $self = shift;
    my $location = shift;

    if(defined $self->{Objects}->{ $location })
    {
        return $self->{Objects}->{ $location }->{Object};
    }

    return '0';
}

sub setDisplayPicture
{
    my $self = shift;
    my $filename = shift || undef;

    if( !defined $filename )
    {
        $self->{CurrentDP} = '';
        $self->{MSN}->getNotification()->setUserInfo();
        return 1;
    }

    if( $filename !~ /\.png$/ && $filename !~ /\.gif$/ && $filename !~ /\.jpg$/ )
    {
        return $self->error( "Unsupported file type, .png, .gif or .jpg required!" );
    }

    my $location = 'dat'. time .'.tmp';
    ($self->{Objects}->{ $location }->{Object}, $self->{Objects}->{ $location }->{Data}) = $self->createObject( $filename, $location, 3 );
    $self->{MSN}->getNotification()->setUserInfo();
    $self->{CurrentDP} = $location;

    return 1;
}

sub createObject
{
    my $self = shift;
    my $file = shift || return $self->error('File location is required!');
    my $location = shift || return $self->error('Unique identifier is required');
    my $type = shift ||  return $self->error('Type is required!');

    my $data = '';

    #open( DP, $file ) || return $self->error( "Could not find the file '$file'" );
    #binmode(DP); while( <DP> ) { $data .= $_; } close(DP);

    $self->{MSN}->getNotification()->{SOAP}->CreateDocument($file, 'UserTileStatic');
    $self->{MSN}->getNotification()->{SOAP}->GetProfile();

    # SHA1D and the Display Picture Data
    my $sha1d = sha1_base64( $data ) . '=';

    # Compile the object from its keys + sha1d
    my $object = 'Creator="'. $self->{MSN}->{Handle} .'" Type="3" SHA1D="'. $sha1d .'" Size="'. (-s $file) .'" Location="'. $location .'" Friendly="PwAAAA=="';

    # SHA1C - this is a checksum of all the key value pairs
    #my $sha1c = $object =~ s/(\"=\s)*//g;
    #$sha1c = sha1_base64( $sha1c ) . '=';

    # Put it all in its nice msnobj wrapper.
    $object = '&lt;msnobj ' . $object;
    #$object .= ' SHA1C="' . $sha1c;
    $object .= '/&gt;';

    return ( $object, $data );
}

return 1;
__DATA__
